package com.toocms.jinjuhang.ui.commodity.detils

import android.animation.ObjectAnimator
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.view.View
import androidx.annotation.ColorInt
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.graphics.drawable.DrawableCompat
import androidx.core.widget.NestedScrollView
import com.blankj.utilcode.util.ColorUtils
import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.ResourceUtils
import com.toocms.jinjuhang.R
import com.toocms.jinjuhang.BR
import com.toocms.jinjuhang.databinding.FgtCommodityDetailsBinding
import com.toocms.jinjuhang.ui.base.BaseFgt
import com.toocms.tab.TooCMSApplication
import kotlin.math.abs

class CommodityDetailsFgt : BaseFgt<FgtCommodityDetailsBinding, CommodityDetailsModel>() {
    val backDrawable by lazy {
        ResourceUtils.getDrawable(R.mipmap.icon_arrow_left_black)
    }
    val shareDrawable by lazy {
        ResourceUtils.getDrawable(R.mipmap.icon_share_black)
    }
    val cartDrawable by lazy {
        ResourceUtils.getDrawable(R.mipmap.icon_cart_gray)
    }

    override fun onFragmentCreated() {
        topBar.visibility = View.GONE
        binding.backIv.setImageDrawable(changeDrawableColor(backDrawable, 0xFF000000.toInt()))
        binding.cartIv.setImageDrawable(changeDrawableColor(cartDrawable, 0xFF000000.toInt()))
        binding.shareIv.setImageDrawable(changeDrawableColor(shareDrawable, 0xFF000000.toInt()))
        binding.nsv.setOnScrollChangeListener(NestedScrollView.OnScrollChangeListener { _, _, scrollY, _, _ ->
            //滑动完全改变颜色的高度界限
            val limit = binding.bannerSib.height - binding.toolbar.height
            //计算比例
            val percentage = if (1f >= abs(scrollY.toFloat() / limit.toFloat()))
                abs(scrollY.toFloat() / limit.toFloat())
            else
                1f
            //修改标题栏标题及图标颜色
            val titleBackgroundColor = Color.argb(
                (255 * percentage).toInt(),
                255,
                255,
                255
            )
            val iconColor = Color.rgb(
                (255 * (1f - percentage)).toInt(),
                (255 * (1f - percentage)).toInt(),
                (255 * (1f - percentage)).toInt()
            )
            val qmuiTopbarTextColor = ColorUtils.getColor(R.color.qmui_topbar_text_color)
            val titleColor = Color.argb(
                (255 * percentage).toInt(),
                Color.red(qmuiTopbarTextColor),
                Color.green(qmuiTopbarTextColor),
                Color.blue(qmuiTopbarTextColor)
            )
            binding.toolbar.setBackgroundColor(titleBackgroundColor)
//            binding.backIv.setImageDrawable(changeDrawableColor(backDrawable, iconColor))
//            binding.cartIv.setImageDrawable(changeDrawableColor(cartDrawable, iconColor))
//            binding.shareIv.setImageDrawable(changeDrawableColor(shareDrawable, iconColor))
            binding.titleTv.setTextColor(titleColor)
        })
    }

    override fun getLayoutResId() = R.layout.fgt_commodity_details

    override fun getVariableId() = BR.commodityDetailsModel

    override fun viewObserver() {
        viewModel.extraFilterSingleLiveEvent.observe(this, this::changeExtraFilterOption)
        }

    /**
     * 改变Drawable颜色
     */
    private fun changeDrawableColor(drawable: Drawable, @ColorInt color: Int) =
        drawable.mutate().apply {
            DrawableCompat.setTint(drawable, color)
        }

    private fun changeExtraFilterOption(filter: ExtraFilter) {
        var extraFilterPointerView =
            binding.cl.getViewById(R.id.commodity_extra_filter_pointer_view)
        if (null == extraFilterPointerView) {
            extraFilterPointerView = View(requireContext()).apply {
                id = R.id.commodity_extra_filter_pointer_view
                layoutParams =
                    ConstraintLayout.LayoutParams(ConvertUtils.dp2px(30f), ConvertUtils.dp2px(2f))
                        .apply {
                            bottomToBottom = R.id.barrier0
                            leftToLeft = R.id.cl
                            setMargins(0, 0, 0, ConvertUtils.dp2px(5f))
                            left =
                                binding.productDetailsTv.left + binding.productDetailsTv.width / 2 - width / 2
                        }
                background = ResourceUtils.getDrawable(R.drawable.shape_sol_clr_main_cor_10dp)
            }
            binding.cl.addView(extraFilterPointerView)
        }
        extraFilterPointerView.visibility = binding.extraFilterGroup.visibility
        when (filter) {
            ExtraFilter.PRODUCT_DETAILS -> {
                binding.productDetailsTv.isSelected = true
                binding.displayQualificationTv.isSelected = false
                ObjectAnimator.ofFloat(
                    extraFilterPointerView,
                    "translationX",
                    extraFilterPointerView.translationX,
                    (binding.productDetailsTv.left + binding.productDetailsTv.width / 2 - extraFilterPointerView.width / 2).toFloat()
                )
                    .setDuration(500L)
                    .start()
            }
            ExtraFilter.DISPLAY_QUALIFICATION -> {
                binding.productDetailsTv.isSelected = false
                binding.displayQualificationTv.isSelected = true
                ObjectAnimator.ofFloat(
                    extraFilterPointerView,
                    "translationX",
                    extraFilterPointerView.translationX,
                    (binding.displayQualificationTv.left + binding.displayQualificationTv.width / 2 - extraFilterPointerView.width / 2).toFloat()
                )
                    .setDuration(500L)
                    .start()
            }
        }
    }

    override fun getViewModel() =
        CommodityDetailsModel(TooCMSApplication.getInstance(), requireArguments())
}